package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Cus;
import com.hopu.service.CusService;
import com.hopu.util.ResultUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/custom")
@RestController
public class CusController {
    /*
    客户登记
     */
    @Resource
    private CusService cusServiceImpl;


    @PostMapping("/findAllCustom")
    public ResultUtils findAllCustom (@RequestParam(value = "current",defaultValue = "1") Integer current) {
        try {
            PageHelper.startPage(current,6);
            List<Cus> allCustom = cusServiceImpl.findAllCustom();
            PageInfo pageInfo = new PageInfo(allCustom);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("insCus")
    public ResultUtils insCus(@RequestParam(value = "cname",required = true)String cname,
                              @RequestParam(value = "csex",required = true)String csex,
                              @RequestParam(value = "ctel",required = true)String ctel,
                              @RequestParam(value = "ctel1",required = true)String ctel1,
                              @RequestParam(value = "ccard",required = true)String ccard){
        int num = cusServiceImpl.insCus(cname,csex,ctel,ctel1,ccard);
        System.out.println(num);
        if (num==0){
            return new ResultUtils(400,"ERROR",null);
        }
        return new ResultUtils(200,"SUCCESS",1);
    }

    @PostMapping("findCusById")
    public ResultUtils findCusById(int cid){
        Cus cus = cusServiceImpl.findCusById(cid);
        return new ResultUtils(200,"查询单条数据成功",cus);
    }

    @PostMapping("uppCus")
    public ResultUtils uppCus(Cus cus){

        int cus1 = cusServiceImpl.uppCus(cus);
        if (cus1<0){
            return new ResultUtils(400,"ERROR",null);
        }
        return new ResultUtils(200,"SUCCESS",1);

    }
    @PostMapping("findCus")
    private ResultUtils findCus(@RequestParam(value = "ccard",required = false) String ccard,
                                 @RequestParam(value = "cid",required = false) Integer cid){
        System.out.println(ccard+"----"+cid);
        Cus cus = cusServiceImpl.findCus(ccard,cid);
        System.out.println(cus);
        if (!ObjectUtils.isEmpty(cus)) {
            return new ResultUtils(200, "SUCCESS", cus);
        }
        return new ResultUtils(400,"ERROR",cus);
    }
}
